	*------------------------------------------------------------------------------------------------------------%	
	* Figure D8: Effect of large retailer $15 MW on wages at other employers in CZ, non-policy gap
	*------------------------------------------------------------------------------------------------------------%	

	* Load nonpolicy firm data
	use using  "$data/cb/stacked_nonpolicy_firm_dataset.dta" if mw==15, clear
		drop if wage_30==tot_emp // no workers below $30
  	keep if inrange(etime, -12, 11)
	keep if balanced_long==1
	drop if trt_exp==4
	drop if cmp==13481	

	global wage ln_avg_wage_exact
	la var ln_avg_wage_exact "log average hourly wage"
	
	* Program ------------------------------------------------------------------
	
	* Define event study program
	cap program drop vmw_eventstudy
	program define vmw_eventstudy
		
		args outcome ytitle restriction expname controls subdirectory filename
		
		if !regexm("`filename'", "fig") {
			local filename "`outcome'_`expname'"
		}
		
		reghdfe `outcome' $common if balanced_short == 1 & (`restriction'), absorb(`controls') vce(cluster cz)
		PlotCoeff,  title(" ") xline(-0.5) xline(-2.5) vrs("T_") yti(`ytitle') name(`expname')
		graph export "$figures_tables/`subdirectory'`filename'.pdf", replace  
		
	end	

	cap program drop vmw_eventstudy_wage
	program define vmw_eventstudy_wage
		
		args outcome ytitle restriction expname controls subdirectory filename
		
		if !regexm("`filename'", "fig") {
			local filename "`outcome'_`expname'"
		}
		
		reghdfe `outcome' $common if balanced_short == 1 & (`restriction'), absorb(`controls') vce(cluster cz)
		PlotCoeff,  title(" ") xline(-0.5) xline(-2.5) vrs("T_") yti(`ytitle') name(`expname') ylabel(-.002 0 .002 .004 .006 .008 .01)
		graph export "$figures_tables/`subdirectory'`filename'.pdf", replace  
		
	end			

	* Run ----------------------------------------------------------------------
	preserve 
	keep if mw==15
		
	egen std_np_gap=std(np_long_gap)
	replace T=std_np_gap // Change treatment var to non-policy gap

	* Create interaction terms between trtvar and timevar, common to both period, omit -1
	EHTrtVars, prefix("T_") trtvar("T") omit("m1") timevar("etime")
	local common "`r(trtvars)'" 
	di wordcount("`common'") " treatment effects: `common'"
	global common "`common'"
			
	* Fig D8
	local varname: variable label $wage
	vmw_eventstudy_wage  $wage "Effect on `varname'" "mw==15" "figd7" "i.cmp_company_code#i.cz#i.trt_exp i.cmp_company_code#i.trt_exp i.cz#i.trt_exp i.etime#i.trt_exp" "appendix/" "figd8a_spillover_wage_15_all_npgap"
	tab trt_exp if e(sample)==1
	restore

	preserve 
	keep if mw==15 & sample_ind==1
	
	egen std_np_gap=std(np_long_gap)
	replace T=std_np_gap // Change treatment var to non-policy gap

	* Create interaction terms between trtvar and timevar, common to both period, omit -1
	EHTrtVars, prefix("T_") trtvar("T") omit("m1") timevar("etime")
	local common "`r(trtvars)'" 
	di wordcount("`common'") " treatment effects: `common'"
	global common "`common'"
	
	* Fig D8
	local varname: variable label $wage
	vmw_eventstudy_wage $wage "Effect on `varname'" "mw==15 & sample_ind==1" "figd7" "i.cmp_company_code#i.cz#i.trt_exp i.cmp_company_code#i.trt_exp i.cz#i.trt_exp i.etime#i.trt_exp" "appendix/" "figd8b_spillover_wage_15_sample_ind_npgap"
	tab trt_exp if e(sample)==1
	restore
	
	preserve 
	keep if mw==15 & (poaching_estab==1 | feeder_estab==1)
	
	egen std_np_gap=std(np_long_gap)
	replace T=std_np_gap // Change treatment var to non-policy gap

	* Create interaction terms between trtvar and timevar, common to both period, omit -1
	EHTrtVars, prefix("T_") trtvar("T") omit("m1") timevar("etime")
	local common "`r(trtvars)'" 
	di wordcount("`common'") " treatment effects: `common'"
	global common "`common'"
	
	* Fig D8
	local varname: variable label $wage
	vmw_eventstudy_wage $wage "Effect on `varname'" "mw == 15 & (poaching_estab==1 | feeder_estab==1)" "figd7" "i.cmp_company_code#i.cz#i.trt_exp i.cmp_company_code#i.trt_exp i.cz#i.trt_exp i.etime#i.trt_exp" "appendix/" "figd8c_spillover_wage_15_pos_flows_npgap"
	tab trt_exp if e(sample)==1
	restore

	*------------------------------------------------------------------------------------------------------------%	
	* Figure D9: Effect of large retailer $15 MW on wages at other employers in CZ, non-policy gap, detrended
	*------------------------------------------------------------------------------------------------------------%	
	use using  "$data/cb/stacked_nonpolicy_firm_dataset.dta" if mw==15, clear
  	keep if inrange(etime, -12, 11)
	keep if balanced_long==1
	drop if trt_exp==4
	drop if cmp==13481	
	
	global wage ln_avg_wage_exact
	la var ln_avg_wage_exact "log average hourly wage"

	* Define event study program
	cap program drop vmw_eventstudy
	program define vmw_eventstudy
		
		args outcome ytitle restriction expname controls subdirectory filename
		
		if !regexm("`filename'", "fig") {
			local filename "`outcome'_`expname'"
		}
		
		reghdfe `outcome' $common if balanced_short == 1 & (`restriction'), absorb(`controls') vce(cluster cz)
		PlotCoeff_detrend,  title(" ") xline(-0.5) xline(-2.5) vrs("T_") yti(`ytitle') name(`expname')
		graph export "$figures_tables/`subdirectory'`filename'.pdf", replace  
		
	end	

	cap program drop vmw_eventstudy_wage
	program define vmw_eventstudy_wage
		
		args outcome ytitle restriction expname controls subdirectory filename
		
		if !regexm("`filename'", "fig") {
			local filename "`outcome'_`expname'"
		}
		
		reghdfe `outcome' $common if balanced_short == 1 & (`restriction'), absorb(`controls') vce(cluster cz)
		PlotCoeff_detrend,  title(" ") xline(-0.5) xline(-2.5) vrs("T_") yti(`ytitle') name(`expname') ylabel(-.002 0 .002 .004 .006 .008 .01)
		graph export "$figures_tables/`subdirectory'`filename'.pdf", replace  
		
	end			

	* Run ----------------------------------------------------------------------
	
	preserve 
	keep if mw==15
	
	egen std_np_gap=std(np_long_gap)
	replace T=std_np_gap // Change treatment var to non-policy gap

	* Create interaction terms between trtvar and timevar, common to both period, omit -1
	EHTrtVars, prefix("T_") trtvar("T") omit("m1") timevar("etime")
	local common "`r(trtvars)'" 
	di wordcount("`common'") " treatment effects: `common'"
	global common "`common'"
			
	* Fig D9
	local varname: variable label $wage
	vmw_eventstudy_wage  $wage "Effect on `varname'" "mw==15" "figd8" "i.cmp_company_code#i.cz#i.trt_exp i.cmp_company_code#i.trt_exp i.cz#i.trt_exp i.etime#i.trt_exp" "appendix/" "figd9a_spillover_wage_15_all_npgap_dt"
	tab trt_exp if e(sample)==1
	restore

	preserve 
	keep if mw==15 & sample_ind==1
	
	egen std_np_gap=std(np_long_gap)
	replace T=std_np_gap // Change treatment var to non-policy gap

	* Create interaction terms between trtvar and timevar, common to both period, omit -1
	EHTrtVars, prefix("T_") trtvar("T") omit("m1") timevar("etime")
	local common "`r(trtvars)'" 
	di wordcount("`common'") " treatment effects: `common'"
	global common "`common'"
	
	* Fig D9
	local varname: variable label $wage
	vmw_eventstudy_wage $wage "Effect on `varname'" "mw==15 & sample_ind==1" "figd8" "i.cmp_company_code#i.cz#i.trt_exp i.cmp_company_code#i.trt_exp i.cz#i.trt_exp i.etime#i.trt_exp" "appendix/" "figd9b_spillover_wage_15_sample_ind_npgap_dt"
	tab trt_exp if e(sample)==1
	restore
	
	preserve 
	keep if mw==15 & (poaching_estab==1 | feeder_estab==1)
	
	egen std_np_gap=std(np_long_gap)
	replace T=std_np_gap // Change treatment var to non-policy gap

	* Create interaction terms between trtvar and timevar, common to both period, omit -1
	EHTrtVars, prefix("T_") trtvar("T") omit("m1") timevar("etime")
	local common "`r(trtvars)'" 
	di wordcount("`common'") " treatment effects: `common'"
	global common "`common'"
	
	* Fig D9
	local varname: variable label $wage
	vmw_eventstudy_wage $wage "Effect on `varname'" "mw == 15 & (poaching_estab==1 | feeder_estab==1)" "figd8" "i.cmp_company_code#i.cz#i.trt_exp i.cmp_company_code#i.trt_exp i.cz#i.trt_exp i.etime#i.trt_exp" "appendix/" "figd9c_spillover_wage_15_pos_flows_npgap_dt"
	tab trt_exp if e(sample)==1
	restore
	
	*------------------------------------------------------------------------------------------------------------%	
	* Figure D10: Effect of large retailer CA $12 MW on non-policy establishment wages in CZ, non-policy gap
	*------------------------------------------------------------------------------------------------------------%		
	use "$data/cb/clean_nonpolicy_firm_cz.dta", clear
	keep if stateabbrv == "CA"
	drop if wage_30==tot_emp // no workers below $30

	* Set state MW parameters
	local mw=12
	local ca_mw_date=708
	gen etime=mdate-`ca_mw_date'
	gen mw=`mw'
	local bmw=`mw'-1
	local amw=`mw'-1
		
	* Restrict to within a year of event
	local event_start = -12
	local event_end = 11
	keep if inrange(etime, `event_start',`event_end')
	
	egen spell_cmp=group(spell_id cmp_company_code)	// create a spell X company id
	egen estab_id = group(spell_id cmp_company_code cz) // create an etablishment id, consisting of spell X company X cz
	
	*** RESTRICT TO 12-MONTH BALANCED PANEL OF ESTABLISHMENTS
	
	* Keep in-sample establishment-spell pairs that with at least 10 employees each month
	bysort estab_id mdate: egen est_emp = sum(tot_emp)
	foreach num of numlist 1/12 {
		gen e_m`num'_temp = etime == -`num' & est_emp >= 10
		bysort cz cmp spell_id: egen e_m`num' = max(e_m`num'_temp)
	}
	foreach num of numlist 0/11 {
		gen e_`num'_temp = etime == `num' & est_emp >= 10
		bysort cz cmp spell_id: egen e_`num' = max(e_`num'_temp)
	}
	gen balanced_long = min(e_m12, e_m11, e_m10, e_m9, e_m8, e_m7, e_m6, e_m5, e_m4, e_m3, e_m2, e_m1, e_0, e_1, e_2, e_3, e_4, e_5, e_6, e_7, e_8, e_9, e_10, e_11) // Create indicator for 24-month balanced panel
	gen balanced_short = min(e_m6, e_m5, e_m4, e_m3, e_m2, e_m1, e_0, e_1, e_2, e_3, e_4, e_5)
	* keep if balanced_short == 1 // MEA EDIT
	drop e_*
	
	* Count CZs per company
	egen cz_unique = tag(cz cmp_company_code)
	egen tot_cz_count=total(cz_unique),by(cmp_company_code)
	
	* Generate outcomes equal to share of workers paid below, at and above minimum wage
	gen share_wage_ltmw=share_wage_lt`mw'
	gen share_wage_mw=wage_`mw'/tot_emp
	gen share_wage_gtmw=share_wage_gt`mw'/tot_emp
	
	* Create gap for non-policy establishments	
	* Specify start and end dates for gap and share measures
	local gap_measure_start=-12
	di `gap_measure_start'
	local gap_measure_end=`gap_measure_start'+6
	di `gap_measure_end'

	* For wage bins below the minimum wage, the gap is the number of workers per bin times the value of the bin, only at the policy company
	forval i=7(1)`bmw'{ 
		local n = `mw'-`i'
		gen gap_`n'=(wage_`i')*`n'
	}

	* For wage bins at or above the minimum wage, the gap is set to 0
	forval i=`mw'(1)29{ 
		gen gap_`i'=0
	}

	* The denominator is the total wage bill - sum of number of workers per bin times the value of the bin
	forval i=7(1)29{ 
		gen sumwage_`i'=wage_`i'*`i'
		gen sumwagelg_`i'=wage_`i'*`i'
		replace sumwagelg_`i'= 0 if balanced_long == 0
	}	

	* Gap numerator is sum of gap per bin
	egen sum_gap=rowtotal(gap_*)

	* Denominator for gap is sum of wage bill per bin (numerator for avg. wage)
	egen sum_wage=rowtotal(sumwage_*)
	egen sum_wage_lg=rowtotal(sumwagelg_*)

	* Final gap measure is numerator divided by denominator
	gen gap=sum_gap/sum_wage	
	qui replace gap=. if !inrange(etime,`gap_measure_start',`gap_measure_end') 	
	qui egen np_gap=mean(gap), by(cz cmp_company_code) 
	gen gap_lg=sum_gap/sum_wage_lg	
	qui replace gap_lg=. if !inrange(etime,`gap_measure_start',`gap_measure_end') 	
	qui egen np_gap_lg=mean(gap_lg), by(cz cmp_company_code) 
	
	* Create placebo gap measure	
	* Specify start and end dates for gap measure
	local placebo_gap_start=-12
	di `placebo_gap_start'
	local placebo_gap_end=`placebo_gap_start'+3
	di `placebo_gap_end'

	* Final gap measure is numerator divided by denominator
	gen placebo_gap=sum_gap/sum_wage 
	qui replace placebo_gap =. if !inrange(etime,`placebo_gap_start',`placebo_gap_end') 		
	qui egen np_placebo_gap=mean(placebo_gap), by(cz) 			

	drop gap_* sumwage*

	* Regressions
	keep if balanced_long==1
	
	* Generate log of wage variables
	cap	gen ln_avg_wage_exact=log(avg_wage_exact_gt8)
	cap gen ln_avg_wage_exact=log(ln_avg_wage_exact)
	cap gen ln_avg_wage_bins=log(avg_wage_bins)
	
	global wage ln_avg_wage_exact
	
	* Drop outlier establishments
	qui sum np_gap,d
	local np_gap_p95=r(p95)
	
	egen std_np_gap=std(np_gap)
	gen T=std_np_gap // Change treatment var to non-policy gap

	* Program ------------------------------------------------------------------
	
	* Create interaction terms between trtvar and timevar, common to both period, omit -1
	EHTrtVars, prefix("T_") trtvar("T") omit("m1") timevar("etime")
	local common "`r(trtvars)'" 
	di wordcount("`common'") " treatment effects: `common'"
	global common "`common'"
		
	* Define event study program
	cap program drop vmw_eventstudy
	program define vmw_eventstudy
		
		args outcome ytitle restriction expname controls subdirectory filename
		
		if !regexm("`filename'", "fig") {
			local filename "`outcome'_`expname'"
		}
		
		reghdfe `outcome' $common if balanced_long == 1 & (`restriction'), absorb(`controls') vce(cluster cz)
		PlotCoeff,  title(" ") xline(-0.5) xline(-2.5) vrs("T_") yti(`ytitle') name(`expname') 
		graph export "$figures_tables/`subdirectory'`filename'.pdf", replace  
		
	end	

	*"Mean Gap = `np_gap_mean'" "Std. Dev. Gap = `np_gap_std'", ring(0) pos(5)
	
	* Run ----------------------------------------------------------------------
	
	* Fig D10
	local varname: variable label $wage
	vmw_eventstudy  $wage "Effect on `varname'" "mw==12 &	np_gap<`np_gap_p95'" "figd8" "i.cmp_company_code#i.cz i.cmp_company_code i.cz i.etime" "appendix/" "figd10_spillover_wage_12_all_npgap" 

